package com.ruoyi.web.controller.sms;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.jianqiao.delivery.mail.AESUtil;
import com.jianqiao.delivery.mail.CodeUtil;
import com.jianqiao.delivery.sms.domain.RegisterParam;
import com.jianqiao.delivery.sms.domain.VerityParam;
import com.jianqiao.delivery.util.RedisUtils;
import com.jianqiao.delivery.util.SmsUtils;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * author: cyx
 * create: 2024/12/2 10:53
 **/
@Slf4j
@RestController
@RequestMapping("/sms")
public class SmsController {

    @Autowired
    private CodeUtil codeUtil;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private SysUserMapper userMapper;

    @PostMapping("/send")
    public AjaxResult send(@RequestBody VerityParam verityParam) {
        String internationalCode = verityParam.getInternationalCode();
        String phone = verityParam.getPhone();

        // 生成验证码
        String code = String.valueOf(codeUtil.generateVerificationCode());

        // 存储验证码到redis,设置过期时间为 5 分钟
        redisCache.deleteObject(phone);
        redisCache.setCacheObject(phone,code,5, TimeUnit.MINUTES);

        JSONObject json = new JSONObject();
        json.put("code",code);

        try {
            SmsUtils.sendSms(internationalCode ,phone ,json.toJSONString());
        } catch (ClientException e) {
            redisCache.deleteObject(phone);
            log.error("号码为：[{}]的短信发送失败",phone,e);
            return AjaxResult.success("发送失败："+e.getMessage());
        }
        return AjaxResult.success("发送成功，请注意查收");
    }

    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterParam registerParam) {
        String phone = registerParam.getPhone();
        String code = registerParam.getCode();
        String password = registerParam.getPassword();
        //判断手机号是否重复
        SysUser sysUser = userMapper.checkPhoneUnique(phone);
        if (sysUser != null) {
            return AjaxResult.error("该手机号已被注册");
        }

        // 从redis获取验证码
        String originCode = redisCache.getCacheObject(phone);
        // 比较验证码
        if (!code.equals(originCode)){
            return AjaxResult.error();
        } else {
            redisCache.deleteObject(phone);
        }

        // 用户注册
        RegisterBody registerBody = new RegisterBody();
        registerBody.setUsername(phone);
        registerBody.setPassword(password);
        String msg = registerService.gmailRegister(registerBody);
        System.out.println(msg);

        // 用户登录
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.gmailLogin(phone, password);
        ajax.put(Constants.TOKEN, token);

        return ajax;
    }
}
